/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-07-08
 * Time: 15:19
 */


 class ListNode {
     int val;
     ListNode next;
     ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }

class Solution {
    public ListNode reverseList(ListNode head) {
        //当头结点head为空时
        if (head == null) {
            return head;
        }
        //获取第二个节点，将第一个节点的next置为null
        ListNode cur = head.next;
        head.next = null;

        //头插
        while (cur != null) {
            //先获取当前节点的下一个节点
            ListNode curN = cur.next;
            //将当前节点的next指向头结点（头插）
            cur.next = head;
            //将head更新为新头插的节点
            head = cur;
            //更新cur，继续头插
            cur = curN;
        }

        //返回逆置后的头结点
        return head;
    }
}
public class Test {
    public static void main(String[] args) {

    }
}
